import time
from threading import Thread

import win32gui
from pynput.keyboard import Key, Listener

from screen import Screen

# from utils.init import sc
# from utils.tasks.control_run import ControlRun


turn_px=200

class TurnTask():
    def __init__(self):
        super().__init__()
        self.last_time = time.time()
        self.cur_steps = ''

    def turn_left(self):
        sc.turn_left(px=turn_px)  # 向左转

    def turn_right(self):
        sc.turn_right(px=turn_px)  # 向右转

    def turn_up(self):
        sc.turn_up(px=turn_px)  # 向上

    def turn_down(self):
        sc.turn_down(px=turn_px)  # 向下


def key_event():
    def on_press(key):
        print(f"key... {key}")
        if key == Key.left:
            print("turn_left...")
            task.turn_left()
            return True
        if key == Key.right:
            print("turn_right...")
            task.turn_right()
            return True
        if key == Key.up:
            print("turn_up...")
            task.turn_up()
            return True
        if key == Key.down:
            print("turn_down...")
            task.turn_down()
            return True
        # if key == Key.f11:
        if key == Key.page_down:
            print("...to exit")
            return False
        return None

    with Listener(on_press=on_press) as listener:
        listener.join()


task = TurnTask()
# cr = ControlRun()

def get_active_window_handle():
    hwnd = win32gui.GetForegroundWindow()
    return hwnd

time.sleep(3)
hwnd = get_active_window_handle()
# hwnd = win32gui.FindWindow(None, '原神')
print('窗口句柄:', hwnd)
if not hwnd:
    print('找不到窗口')
    exit(0)
print(f"当前活动窗口句柄: {hwnd}")
sc = Screen(hwnd,is_get_client_pos=False)

# def run_task():
#     task.run()


# def run_cr():
#     cr.run()





if __name__ == "__main__":
    key_thread = Thread(target=key_event)
    key_thread.start()

    # run_task_thread = Thread(target=run_task)
    # run_task_thread.start()
    #
    # run_cr_thread = Thread(target=run_cr)
    # run_cr_thread.start()

